//
//  CLLProgressHUD.swift
//  DemoNavBar
//
//  Created by YangHuan on 2019/12/14.
//  Copyright © 2019 YangHuan. All rights reserved.
//

import Foundation
import MBProgressHUD

final class CLLProgressHUD {
    
    static private var hud: MBProgressHUD?
    static private var pHud: MBProgressHUD?
    
//    会自动消失的提示信息
   static private func mbshow(text: String?,
                              view: UIView?,
                              icon: String?) {
    DispatchQueue.main.async {
          let v = view == nil ? UIApplication.shared.windows.last! : view!
                  self.hud = MBProgressHUD.showAdded(to: v, animated: true)
                  self.hud?.label.text = text
                  if icon == nil {
                      self.hud?.mode = .text
                  } else {
                      self.hud?.mode = .customView
                      self.hud?.customView = UIImageView(image: UIImage(named: icon!))
                  }
                  self.hud?.removeFromSuperViewOnHide = true
                  self.hud?.hide(animated: true, afterDelay: 1)
                  self.hud?.offset = CGPoint(x: 0, y: -20)
            }
    }
    
//    自定义动画 加载中
      static private func mbShow(view: UIView?,
                                 message: String?) {
          DispatchQueue.main.async {
//              let v = view == nil ? UIApplication.shared.windows.last! : view!
              let v = view == nil ? UIApplication.shared.windows.last! : view!
              self.hud?.hide(animated: false)
              self.hud = MBProgressHUD.showAdded(to: v, animated: true)
              self.hud?.mode = .customView
              self.hud?.label.text = message
              let image = UIImage(named: "loading_1")?.withRenderingMode(.alwaysTemplate)
              let mainImageView = UIImageView(image: image)
              mainImageView.animationImages = [#imageLiteral(resourceName: "loading_1"),#imageLiteral(resourceName: "loading_2"),#imageLiteral(resourceName: "loading_3"),#imageLiteral(resourceName: "loading_4"),#imageLiteral(resourceName: "loading_5"),#imageLiteral(resourceName: "loading_6"),#imageLiteral(resourceName: "loading_7"),#imageLiteral(resourceName: "loading_8"),#imageLiteral(resourceName: "loading_9"),#imageLiteral(resourceName: "loading_10")]
              mainImageView.animationDuration = 0.5
              mainImageView.animationRepeatCount = 0
              mainImageView.startAnimating()
              self.hud?.customView = mainImageView
              self.hud?.animationType = .fade
          }
      }
    
    
    
    
    
}
// MARK: - 项目中调用的接口
extension CLLProgressHUD {
      static func showError(status: String) {
          self.showError(status)
      }
      
      /// 错误信息展示
      static func showError(_ error: Any?, view: UIView? = nil) {
          guard let err = error as AnyObject? else { return }
          var msg: String?
          if err is NSError {
              msg = getErrorMessage(error: err as! NSError) //showReturnErrorMsg(err as! NSError)
          } else if err is String {
              msg = err as? String
          } else if err.isKind(of: NSString.classForCoder()) {
              msg = err as? String
          } else {
              msg = "ERROR!"
          }
          mbshow(text: msg, view: view, icon: "abnormal_")
      }
      
      /// 成功提示
      static func showSuccessMessage(_ msg: String, view: UIView? = nil) {
        mbshow(text: msg, view: view, icon: "success_")
         
      }
      
      static func showSuccessMessage(_ msg: String, view: UIView? = nil, _ completion:(() -> Void)? = nil) {
        mbshow(text: msg, view: view, icon: "success_")
          DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double((__int64_t)(1.2 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)) {
              completion?()
          }
      }
      
      /// 有加载动画的message
      static func showMessages(text: String) {
          self.mbShow(view: nil, message: text)
      }
      
      /// 纯文字提示
      static func showToast(text: String) {
        mbshow(text: text, view: nil, icon: nil)
      }
      
      /// 感叹号提示
      static func showInfo(_ text: String) {
        mbshow(text: text, view: nil, icon: "_info")
        
      }
      
      static func show(view: UIView? = nil) {
          self.mbShow(view: view, message: nil)
      }
      
      static func dismiss() {
          hud?.hide(animated: true)
      }
      
      /// 进度显示
      static func showProgress(progress: CGFloat, view: UIView? = nil, message: String? = nil) {
          DispatchQueue.main.async {
              let v = view == nil ? UIApplication.shared.windows.last! : view!
              if self.pHud == nil {
                  self.pHud = MBProgressHUD.showAdded(to: v, animated: true)
                  self.pHud?.mode = .annularDeterminate
              }
              self.pHud?.label.text = message
              self.pHud?.progress = Float(progress)
              if progress == 1 {
                  self.pHud?.hide(animated: true)
                  self.pHud = nil
              }
          }
      }
    
    
}
